import {
  resetpwd,
  queryAccount,
  saveProfile,
  accountExport,
  rmAccount,
  accountDisable,
} from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'account',

  state: {
    list: [],
    pagination: {
      current: 1,
      pageSize: 10,
      pageSizeOptions: ['10', '20', '30', '40', '50', '80', '100'],
    },
    total: 0,
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryAccount, payload);
      let dataSource = response.amUserManage;
      if (payload) {
        if (payload.sorter) {
          const s = payload.sorter.split('_');

          if (s[1] === 'descend') {
            dataSource = dataSource.sort((prev, next) => method(s[0], next[s[0]], prev[s[0]]));
          } else {
            dataSource = dataSource.sort((prev, next) => method(s[0], prev[s[0]], next[s[0]]));
          }
        }

        let pageSize = 10;
        if (payload.pageSize) {
          pageSize = payload.pageSize * 1;
        }

        const result = {
          list: dataSource,
          total: response.pageInfo.pageTotal,
          pagination: {
            total: response.pageInfo.pageTotal,
            pageSize,
            pageSizeOptions: ['10', '20', '30', '40', '50', '80', '100'],
            current: response.pageInfo.curPageNum || 1,
          },
        };
        yield put({
          type: 'save',
          payload: result,
        });
      }
    },
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response.amUserInfo,
      });
    },
    *resetPwd({ payload, callback }, { call }) {
      const response = yield call(resetpwd, payload);
      if (response?.result?.code !== 1000) {
        message.error('提交出错，请检查');
      } else if (callback) callback();
    },
    *saveprofile({ payload, callback }, { call, put }) {
      const response = yield call(saveProfile, payload);
      if (response?.amSaveUserInfo === 'undefined') {
        message.error('提交出错，请检查');
      } else {
        if (callback) {
          callback();
        }
        yield put({
          type: 'saveCurrentUser',
          payload: response.amSaveUserInfo,
        });
      }
    },
    *remove({ payload, callback }, { call, put }) {
      const { userName } = payload;
      const response = yield call(rmAccount, payload);
      //console.log('rm result', response);
      if (response?.result?.code === 2001) {
        message.error('删除失败，请检查');
      } else {
        yield put({
          type: 'removeAccount',
          payload: { userName },
        });
        if (callback) callback();
      }
    },
    *export({ payload, callback }, { call }) {
      const response = yield call(accountExport, payload);
      //console.log('export result', response);
      if (response?.result?.code === 2001) {
        message.error('提交出错，请检查');
      } else if (callback) callback(response.expAmUserManage.expFileUrl);
    },

    *disabled({ payload, callback }, { call, put }) {
      const response = yield call(accountDisable, payload);
      //console.log('export result', response);
      if (response?.result?.code === 2001) {
        message.error('提交出错，请检查');
      } else {
        yield put({
          type: 'disableAccount',
          payload,
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        total: action.payload.total,
        list: action.payload.list,
        pagination: action.payload.pagination,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          ...action.payload,
        },
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
    removeAccount(state, action) {
      const newlist = state.list.filter(item => item.userName !== action.payload.userName);
      return {
        ...state,
        list: newlist,
      };
    },
    disableAccount(state, action) {
      const index = state.list.findIndex(item => item.userName === action.payload.userName);
      const newlist = [...state.list];
      if (index >= 0) {
        newlist[index].userStatus = newlist[index].userStatus === '禁用' ? '启用' : '禁用';
      }
      return {
        ...state,
        list: newlist,
      };
    },
  },
};
